<?php
/**
 *  Create On 2014-12-19
 *  Author yiwei
 *  QQ:1006629314
 **/
include('common/libs/simplehtmldom/simple_html_dom.php');
class DisplayItemUtil{
	private $mode;//显示模式：0-不启用mathjax 1-启用mathjax 2-字号增强模式(供ios白板显示用)
	private $type; // 0-题目内容，1-题目详情
	private $item;//题目结构
	private $width;//显示宽度
	private $height;//mode为2模式下的分屏高度
	
	public function __construct($item = null){
		$this->item = $item;
	}
	public function init($mode,$type,$json,$width,$height="100"){
		$this->mode = $mode;
		$this->type = $type;
		$this->item = json_decode($json,true);
		$width = str_replace("","px",$width);
		$width = intval($width)-20;
		$this->width = $width."px";//px
		$this->height = $height;
	}
	public function display(){
		//var_dump($this->item);
		//print_r($this->item);
		$str = $this->_parseNode($this->item['nodeTree']);
		$html = $this->_improve($str);
		switch($this->mode){
			case 0:
				$script = '';
				$css =
<<<HTML
	<style>
		body{margin:1px 10px;width:$this->width;word-wrap:break-word; overflow:hidden;}
		li{list-style-type:circle;}
	</style>
HTML;
break;
			case 1:
				$script =
<<<HTML
	<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
HTML;
				$css =
<<<HTML
	<style>
		body{margin:0 10px;width:$this->width;word-wrap:break-word; overflow:hidden;}
		li{list-style-type:circle;}
	</style>
HTML;
break;
			case 2:
				/*
				 <<<HTML
				<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
				<script type="text/x-mathjax-config">
				MathJax.Hub.Config({
						jax: ["input/MathML", "output/HTML-CSS"],
						mml2jax: {
						preview: "mathml"
						},
						"HTML-CSS": {
						scale: 300
						}
						});
				</script>
				HTML;
				*/
				$script =
<<<HTML
	<script type="text/javascript">
		window.onload = function(){
				var plist = document.getElementsByTagName("p");
				for(var p in plist){
        			Resetposition(plist[p]);
    			}  
			}
	
		function ResizeImage(obj){
			var width = obj.width;
			obj.width = 2*width;	
		}
		function Resetposition(obj){
			var pos = obj.offsetTop;
		
			var height = obj.clientHeight;
			var diff = (pos%$this->height+height)-$this->height;
			//alert("pos:"+pos+",height:"+height+",diff:"+diff+",inner:"+obj.innerHTML);
			if(diff>0){
				//var top = obj.style.marginTop;
				//alert(top);
				obj.style.paddingTop = height-diff+50;
			}
		}
	</script>
HTML;
				$css =
<<<HTML
	<style>
		body{margin-top:44px;width:$this->width;font-size:40px;word-wrap:break-word; overflow:hidden;}
		li{list-style-type:circle;}
	</style>
HTML;
				//$html = '<div width="'.(($this->width)/2).'px">'.$html.'</p>';
break;
		}
		echo $script.$css.$html;
	}
	
	private function _parseNode($node){
		$html = '';
		//if($this->mode == 2){
		//	$html .= '<p onload="Resetposition(this)"><font style="font-weight: 900;font-size: 120%;">'.$node['content'].'</font></p>';
		//}else{
			$html .= '<p><font style="font-weight: 900;font-size: 120%;">'.$node['content'].'</font></p>';
		//}
		if($this->type){
			if($node['answer'] !='<p></p>')
			{
				$html .='<p><font style="background:red;color:white;">答案</font></p>';
				$html .=$node['answer'];
			}
			if($node['parse']!='<p></p>')
			{
				$html .='<p><font style="background:#FF9900;color:white;">解析</font></p>';
				$html .=$node['parse']; 
			}
			if($node['knows']){
				$html .='<p><font style="background:green;color:white;">知识点</font></p>';
				$html .='<ul>';
				foreach ($node['knows'] as $val){
					$html .='<li>'.$val['name'].'</li>';
				}
				$html .='</ul>';
			}
			if($node['skill']){
				$html .='<p><font style="background:green;color:white;">解题技巧</font></p>';						
				$html .='<ul>';
				foreach ($node['skill'] as $val){
					$html .='<li>'.$val['name'].'</li>';
				}
				$html .='</ul>';
			}
		}
		//遍历子节点
		$children = $node['children'];
		for($i=0;$i<count($children);$i++){
			$html .= $this->_parseNode($children[$i]);
		}
		return $html;
	}
	
	private function _improve($str){
		$html = str_get_html($str);
		
		foreach ($html->find('img') as $image) {
			//$image->width = '';
			if($this->mode ==2){
				$image->onload = "ResizeImage(this);";
			}
			$image->style = 'max-width:'.$this->width.'px;';
		} 
		return $str;
	}
}




 ?>